package com.xqboss.system.domain.dto;

import com.xqboss.common.core.text.Convert;
import com.xqboss.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Trent
 * @date 2023/9/11
 */
public class IntArrayConfigKey implements SysConfigKey<List<Integer>> {

    private final String key;

    public IntArrayConfigKey(String key){
        this.key = key;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public List<Integer> convertValue(String value) {
        List<Integer> result = new ArrayList<>();
        if(StringUtils.isNotEmpty(value)){
            try{
                String[] split = value.split(",");
                for (String s : split) {
                    result.add(Integer.valueOf(s));
                }
            }catch (Exception ignored){
            }
        }
        return result;
    }

    @Override
    public String getValue(List<Integer> value) {
        if(value == null){
            return null;
        }
        List<String> collect = value.stream().map(String::valueOf).collect(Collectors.toList());
        return String.join(",", collect);
    }
}
